package com.yulong.web.controller;

import com.github.pagehelper.PageInfo;
import com.yulong.model.pojo.UmsMenu;
import com.yulong.model.pojo.UmsResource;
import com.yulong.model.pojo.UmsRole;
import com.yulong.web.server.UmsRoleServer;
import com.yulong.web.utils.ResponseMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api("后台用户角色管理")
@RequestMapping("role")
public class UmsRoleController {

    @Autowired
    private UmsRoleServer umsRoleServer;

    @ApiOperation("添加角色")
    @PostMapping(value = "/create")
    @ResponseBody
    public ResponseMap create(@RequestBody UmsRole role) {
        role.setCreateTime(new Date());
        role.setSort(0);
        role.setStatus(0);

        int count = umsRoleServer.create(role);
        if (count > 0) {
            return ResponseMap.ok(count);
        }
        return ResponseMap.errorMsg("创建角色失败");
    }


    @ApiOperation("批量删除角色")
    @PostMapping(value = "/delete")
    @ResponseBody
    public ResponseMap delete(@RequestBody List<Long> ids) {
        int count = umsRoleServer.delete(ids);
        if (count > 0) {
            return ResponseMap.ok(count);
        }
        return ResponseMap.errorMsg("删除角色失败");
    }


    @ApiOperation("修改角色")
    @PostMapping(value = "/update/{id}")
    @ResponseBody
    public ResponseMap update(@PathVariable Long id, @RequestBody UmsRole role) {
        int count = umsRoleServer.update(id,role);
        if (count > 0) {
            return ResponseMap.ok(count);
        }
        return ResponseMap.errorMsg("修改角色失败");
    }


    @ApiOperation("获取所有角色")
    @GetMapping(value = "/listAll")
    @ResponseBody
    public ResponseMap listAll() {
        List<UmsRole> list = umsRoleServer.list();
        return ResponseMap.ok(list);
    }



    @ApiOperation("根据角色名称分页获取角色列表")
    @PostMapping(value = "/list")
    @ResponseBody
    public ResponseMap list(@RequestParam String keyword, @RequestParam Integer pageSize,@RequestParam Integer pageNum) {
        PageInfo<UmsRole> list = umsRoleServer.list(keyword, pageSize, pageNum);
        return ResponseMap.ok(list);
    }

    @ApiOperation("获取角色相关菜单")
    @GetMapping(value = "/listMenu/{roleId}")
    @ResponseBody
    public ResponseMap listMenu(@PathVariable(value = "roleId") Long roleId) {
        List<UmsMenu> umsMenus = umsRoleServer.listMenu(roleId);
        return ResponseMap.ok(umsMenus);
    }

    @ApiOperation("获取当前用户的资源分类信息")
    @GetMapping(value = "/listResource/{roleId}")
    @ResponseBody
    public ResponseMap listResource(@PathVariable(value = "roleId") Long roleId) {
        List<UmsResource> umsResources = umsRoleServer.listResource(roleId);
        return ResponseMap.ok(umsResources);
    }


}
